import pygame
import random

# 初始化Pygame
pygame.init()

# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# 设置屏幕尺寸import pygame
import random
import math

# 初始化Pygame
pygame.init()

# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# 设置屏幕尺寸
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("弹珠游戏")

# 定义弹珠类
class Marble(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)
        pygame.draw.circle(self.image, color, (width // 2, height // 2), width // 2)
        self.rect = self.image.get_rect()
        self.rect.x = random.randint(0, SCREEN_WIDTH - width)
        self.rect.y = random.randint(0, SCREEN_HEIGHT - height)
        self.change_x = random.randint(-5, 5)
        self.change_y = random.randint(-5, 5)
        self.radius = width // 2

    def update(self):
        self.rect.x += self.change_x
        self.rect.y += self.change_y
        if self.rect.x <= 0 or self.rect.x >= SCREEN_WIDTH - self.rect.width:
            self.change_x *= -1
        if self.rect.y <= 0 or self.rect.y >= SCREEN_HEIGHT - self.rect.height:
            self.change_y *= -1

# 定义玩家挡板类
class Paddle(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface([100, 10])
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.x = SCREEN_WIDTH // 2 - 50
        self.rect.y = SCREEN_HEIGHT - 20

    def update(self):
        pos = pygame.mouse.get_pos()
        self.rect.x = pos[0] - 50
        if self.rect.x < 0:
            self.rect.x = 0
        if self.rect.x > SCREEN_WIDTH - 100:
            self.rect.x = SCREEN_WIDTH - 100

# 创建弹珠组和玩家挡板
all_sprites = pygame.sprite.Group()
marbles = pygame.sprite.Group()
for i in range(10):
    marble = Marble(BLUE, 20, 20)
    all_sprites.add(marble)
    marbles.add(marble)

paddle = Paddle()
all_sprites.add(paddle)

# 初始化分数和字体
score = 0
font = pygame.font.Font(None, 36)

# 游戏主循环
running = True
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    all_sprites.update()

    # 检测弹珠之间的碰撞
    for marble in marbles:
        collided_marbles = pygame.sprite.spritecollide(marble, marbles, False, pygame.sprite.collide_circle)
        for other_marble in collided_marbles:
            if marble != other_marble:
                # 简单的碰撞响应
                marble.change_x, other_marble.change_x = other_marble.change_x, marble.change_x
                marble.change_y, other_marble.change_y = other_marble.change_y, marble.change_y

    # 检测弹珠与挡板的碰撞
    hit_marbles = pygame.sprite.spritecollide(paddle, marbles, False)
    for marble in hit_marbles:
        score += 1
        marble.change_y = -abs(marble.change_y)  # 确保弹珠向上弹起

    # 增加难度
    if pygame.time.get_ticks() % 5000 == 0:  # 每5秒
        for marble in marbles:
            marble.change_x *= 1.1
            marble.change_y *= 1.1

    screen.fill(WHITE)
    all_sprites.draw(screen)

    # 显示分数
    score_text = font.render(f"得分: {score}", True, BLACK)
    screen.blit(score_text, (10, 10))

    pygame.display.flip()

    clock.tick(60)

pygame.quit()

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("弹珠游戏")

# 定义弹珠类
class Marble(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)
        pygame.draw.circle(self.image, color, (width // 2, height // 2), width // 2)
        self.rect = self.image.get_rect()
        self.rect.x = random.randint(0, SCREEN_WIDTH - width)
        self.rect.y = random.randint(0, SCREEN_HEIGHT - height)
        self.change_x = random.randint(-5, 5)
        self.change_y = random.randint(-5, 5)

    def update(self):
        self.rect.x += self.change_x
        self.rect.y += self.change_y
        if self.rect.x <= 0 or self.rect.x >= SCREEN_WIDTH - self.rect.width:
            self.change_x *= -1
        if self.rect.y <= 0 or self.rect.y >= SCREEN_HEIGHT - self.rect.height:
            self.change_y *= -1

# 创建弹珠组
all_sprites = pygame.sprite.Group()
for i in range(10):
    marble = Marble(BLUE, 20, 20)
    all_sprites.add(marble)

# 游戏主循环
running = True
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    all_sprites.update()

    screen.fill(WHITE)
    all_sprites.draw(screen)
    pygame.display.flip()

    clock.tick(60)

pygame.quit()